package com.server.common.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

/**
 * 获取应用根目录工具类
 * 
 * @author 王磊
 *
 */
public class PathUtils {
	private static String webRoot = null;
	static {
		webRoot = getWebrootPath();
	}

	public static String getWebrootPath() {
		String root = PathUtils.class.getResource("/").getFile();
		try {
			root = new File(root).getParentFile().getParentFile()
					.getCanonicalPath();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		root += File.separator;
		return root;
	}

	public static String getRoot() {
		return webRoot;
	}

	/**
	 * 获取配置信息
	 */
	public static Properties getProperties(String fileName) {
		if (StringUtils.isEmpty(fileName))
			return null;
		Properties properties = null;
		InputStream inputStream = null;
		try {
			inputStream = PathUtils.class.getResourceAsStream("/" + fileName);
			properties = new Properties();
			properties.load(new InputStreamReader(inputStream, "UTF-8"));
		} catch (IOException e) {
		//	LogUtils.getFrameworkLoger().error(e);
			throw new RuntimeException(e);
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (Exception e2) {
					throw new RuntimeException("关闭输入流失败" + e2);
				}
			}
		}
		return properties;
	}
	public static  String getPropertiesKeyValue(String fileName,String key){
		Properties properties = 	PathUtils.getProperties(fileName);
		return properties.getProperty(key);
	}
	public static String getImageURL(){
		return PathUtils.getPropertiesKeyValue("application.properties","imageURL");
	}

	public static String getSaveImagePath(){
		return PathUtils.getPropertiesKeyValue("application.properties","imagePath");
	}
}
